package com.fzt.system.controller;

import com.fzt.common.utils.R;
import com.fzt.framework.utils.TokenUtils;
import com.fzt.system.service.security.CaptchaService;
import com.fzt.system.service.security.LoginService;
import com.fzt.system.domain.vo.AccountLoginVO;
import com.fzt.system.domain.vo.CaptchaVO;
import com.fzt.system.domain.vo.TokenVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *
 * </p>
 *
 * @author 非洲铜
 * @since 2022-12-09
 */
@RestController
@RequestMapping("/sys/auth")
@AllArgsConstructor
public class LoginController {

    private final CaptchaService sysCaptchaService;
    private final LoginService   loginService;

    /**
     * 获取验证码
     */
    @GetMapping("/captcha")
    public R<CaptchaVO> captcha() {
        CaptchaVO captchaVO = sysCaptchaService.generate();
        return R.ok(captchaVO);
    }

    /**
     * 账号登录
     */
    @PostMapping("/login")
    public R<TokenVO> login(@RequestBody AccountLoginVO login) {
        TokenVO token = loginService.loginByAccount(login);
        return R.ok(token);
    }

    /**
     * 登出
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        loginService.logout(TokenUtils.getAccessToken(request));
        return R.ok();
    }
}
